//package cn.com.woxitech.dev.controller.websocket;
//import java.util.ArrayList;
//import java.util.List;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.socket.CloseStatus;
//import org.springframework.web.socket.TextMessage;
//import org.springframework.web.socket.WebSocketSession;
//import org.springframework.web.socket.handler.TextWebSocketHandler;
//
//import com.fasterxml.jackson.databind.JsonNode;
//import com.fasterxml.jackson.databind.node.ArrayNode;
//import com.fasterxml.jackson.databind.node.ObjectNode;
//
//import cn.com.woxitech.dev.common.JSONUtils;
//import cn.com.woxitech.dev.model.gen.WmRoom;
//import cn.com.woxitech.dev.model.gen.WmRoomChathistory;
//import cn.com.woxitech.dev.model.gen.WmUser;
//import cn.com.woxitech.dev.service.RoomService;
//import cn.com.woxitech.dev.service.UserService;  
//  
//public class AppChatEndpointHandler extends TextWebSocketHandler  {  
//  
//	private static final ArrayList<WebSocketSession> users = new ArrayList<>();
//
//	@Autowired
//	private RoomService roomService;
//
//	@Autowired
//	private UserService userService;
//	
//    @SuppressWarnings("deprecation")
//	@Override  
//    protected void handleTextMessage(WebSocketSession session,  TextMessage message) throws Exception {  
//        super.handleTextMessage(session, message);  
//        
//        WmUser wxUser = (WmUser)session.getAttributes().get("userInfo");
//        String roomuuid = (String)session.getAttributes().get("roomuuid");
//         
//        String content = new String(message.asBytes());
//        JsonNode c  = JSONUtils.toJson(content);
//        ObjectNode nc = JSONUtils.jsonInit();
//        String type = c.get("type").asText();
//        
//        boolean saveHistory = true;
//        if(type.equals("keep")){
//        	
//        	return;
//        	
//        }else if(type.equals("login")){
//        	int concurrent = this.roomService.countOnlineUser(roomuuid, true);
//        	WmRoom r = this.roomService.getRoomsByUuid(roomuuid);
//        	
//        	nc.put("concurrent", concurrent);
//        	nc.put("attendcount", this.roomService.addAttendCount(r));
//        	
//        	int likecount_base = r.getLikecount().intValue();
//        	int likecount_now = this.roomService.getRoomLikeCount(roomuuid);
//        	nc.put("like",  likecount_now + likecount_base);
//        	
//    	    //记录最高并发人数
//    	    this.roomService.saveMaxConcurrentUser(roomuuid, concurrent);
//        
//        }else if(type.equals("like")){
//        	
//        	WmRoom r = this.roomService.getRoomsByUuid(roomuuid);
//        	roomService.saveRoomLike(roomuuid, wxUser.getUuid());
//        	int likecount_base = r.getLikecount().intValue();
//        	
//        	nc.put("like",  this.roomService.getRoomLikeCount(roomuuid,wxUser.getUuid()) + likecount_base);
//        	
//        	saveHistory = false;
//        }else if(type.equals("dislike")){
//        	
//        	WmRoom r = this.roomService.getRoomsByUuid(roomuuid);
//        	roomService.deleteRoomLike(roomuuid, wxUser.getUuid());
//        	int likecount_base = r.getLikecount().intValue();
//        	
//        	nc.put("like",  this.roomService.getRoomLikeCount(roomuuid,wxUser.getUuid()) + likecount_base);
//        	saveHistory = false;
//        }else if(type.equals("txt")){
//        	
//        	nc.put("content", c.get("content").asText());
//        	nc.put("msgid", c.get("msgid").asText());
//        	
//        }else if(type.equals("audio")){
//        	
//        	nc.put("newsrc", c.get("newsrc").asText());
//        	nc.put("sec", c.get("sec").asText());
//        	nc.put("msgid", c.get("msgid").asText());
//        	nc.put("ppt", c.get("ppt").asText());
//        	
//        }else if(type.equals("img")){
//        	
//        	nc.put("newsrc", c.get("newsrc").asText());
//        	nc.put("msgid", c.get("msgid").asText());
//        	
//        }
//        
//        nc.put("ischair", this.roomService.countRoomChairs(roomuuid, wxUser.getUuid()));
//
//    	nc.put("headimgurl", wxUser.getWxHeadimgurl());
//    	nc.put("name", wxUser.getName());
//        nc.put("type", type);
//        nc.put("time",System.currentTimeMillis());
//        nc.put("unionid",wxUser.getWxUnionid());
//    	nc.put("useruuid", wxUser.getUuid());
//    	nc.put("roomuuid", roomuuid);
//        
//    	if(saveHistory){
//    		saveHistory(nc);
//    	}
//        
//        
//        String new_content = nc.toString();
//        sendMessageToRoom(roomuuid,  new TextMessage(new_content));
//        
//    }  
//    
//	private void saveHistory(ObjectNode nc) {
//		// TODO Auto-generated method stub
//		String useruuid = nc.get("useruuid").asText();
//	    String roomuuid = nc.get("roomuuid").asText();
//	    long time = System.currentTimeMillis();
//	    String type = nc.get("type").asText();
//	    
//	    String msgid = null;
//	    if(nc.has("msgid")){
//	    	msgid = nc.get("msgid").asText();
//	    }
//	    
//	    String content = null;
//	    if(nc.has("content")){
//	    	content = nc.get("content").asText();
//	    }
//	    
//	    String newsrc = null;
//	    if(nc.has("newsrc")){
//	    	newsrc = nc.get("newsrc").asText();
//	    }
//	    
//	    String sec = null;
//	    if(nc.has("sec")){
//	    	sec = nc.get("sec").asText();
//	    }
//	    
//	    String ppt = null;
//	    if(nc.has("ppt")){
//	    	ppt = nc.get("ppt").asText();
//	    }
//	    
//	    WmRoomChathistory ch = new WmRoomChathistory();
//	    ch.setContent(content);
//	    ch.setNewsrc(newsrc);
//	    ch.setPpt(ppt);
//	    ch.setRoomuuid(roomuuid);
//	    ch.setSec(sec);
//	    ch.setTime(time);
//	    ch.setType(type);
//	    ch.setUseruuid(useruuid);
//	    ch.setMsgid(msgid);
//	    roomService.saveChathistory(ch);
//	}
//	
//
//	//初次链接成功执行
//    @Override
//    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
//        
//        String useruuid =  (String)session.getAttributes().get("useruuid");
//        String roomuuid = (String)session.getAttributes().get("roomuuid");
//        
//        roomService.saveUserOnline(useruuid,roomuuid);
//    	users.add(session);
//    	
//    }
//    
//    @Override
//    public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {
//        if(webSocketSession.isOpen()){
//            webSocketSession.close();
//        }
//        String useruuid =  (String)webSocketSession.getAttributes().get("useruuid");
//        String roomuuid = (String)webSocketSession.getAttributes().get("roomuuid");
//        WmUser wxUser = (WmUser)webSocketSession.getAttributes().get("userInfo");
//        
//        logout(useruuid, roomuuid, wxUser);
//        users.remove(webSocketSession);
//    }
//    
//    @Override
//    public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {
//        String useruuid =  (String)webSocketSession.getAttributes().get("useruuid");
//        String roomuuid = (String)webSocketSession.getAttributes().get("roomuuid");
//        WmUser wxUser = (WmUser)webSocketSession.getAttributes().get("userInfo");
//        
//        logout(useruuid, roomuuid, wxUser);
//        users.remove(webSocketSession);
//    }
//    
//    
//    /**
//     * 给某个Room发送消息
//     *
//     * @param userName
//     * @param message
//     */
//    public void sendMessageToRoom(String roomuuid, TextMessage message) {
//        for (WebSocketSession user : users) {
//            if (((String)user.getAttributes().get("roomuuid")).equals(roomuuid)) {
//                try {
//                    if (user.isOpen()) {
//                        user.sendMessage(message);
//                    }
//                } catch (Exception e) {
//                    //e.printStackTrace();
//                }
//            }
//        }
//    }
////    
////
////    
////    /**
////     * 给某个Room的某个人发送私信
////     *
////     * @param userName
////     * @param message
////     */
////    public void sendMessageToRoomUser(String roomId, String openId, TextMessage message) {
////        for (WebSocketSession user : users) {
////            if (((String)user.getAttributes().get(HJSConstants.WEBSOCKET_ROOMID)).equals(roomId)) {
////                try {
////                    if (user.isOpen() && user.getAttributes().get(HJSConstants.WEBSOCKET_OPENID).equals(openId)) {
////                        user.sendMessage(message);
////                        break;
////                    }
////                } catch (Exception e) {
////                    //e.printStackTrace();
////                }
////            }
////        }
////    }
//    
//
//    private void logout(String useruuid, String roomuuid, WmUser wxUser){
//    	
//        this.roomService.removeUserOnline(useruuid, roomuuid);
//        ObjectNode nc = JSONUtils.jsonInit();
//        nc.put("type", "logout");
//        nc.put("time",System.currentTimeMillis());
//    	nc.put("useruuid", wxUser.getUuid());
//    	nc.put("roomuuid", roomuuid);
//    	
//        saveHistory(nc);
//        
//        String new_content = nc.toString();
//        sendMessageToRoom(roomuuid,  new TextMessage(new_content));
//        
//    }
//} 